/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m006.ui.views;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m005.ui.views.M005View;
import ep.m006.ui.controllers.M006Controller;

/**
 *
 * @author atsi
 */
public class M006View extends AbstractView {
    
    // <editor-fold defaultstate="collapsed" desc="CAPTIONS">
    public static final String CAPTION_PRESC_IDLE = "Εκκρεμείς συνταγές";
    public static final String CAPTION_AMKA = "ΑΜΚΑ";
    public static final String CAPTION_SUBMIT = "Υποβολή";
    public static final String CAPTION_CANCEL = "Άκυρο";
    public static final String CAPTION_BACK = "Επιστροφή";
    public static final String CAPTION_DOCTOR = "Στοιχεία Ιατρού";
    public static final String CAPTION_PATIENT = "Στοιχεία Ασφαλισμένου";
    public static final String CAPTION_DIAGNOSIS = "Διάγνωση";
    public static final String CAPTION_DOSAGE = "Θεραπεία";
    public static final String CAPTION_PRESC = "Στοιχεία Συνταγής";
    public static final String CAPTION_FIRST_NAME = "'Ονομα";
    public static final String CAPTION_SURNAME = "Επώνυμο";
    public static final String CAPTION_ADDRESS = "Διευθυνση";
    public static final String CAPTION_CITY = "Πόλη";
    public static final String CAPTION_TK = "ΤΚ";
    public static final String CAPTION_YEAR_OF_BIRTH = "Ημ/νια Γέννησης.";
    public static final String CAPTION_PHONE = "Τηλέφωνο";
    public static final String CAPTION_INSURANCE = "Ταμείο Ασφάλισης";
    public static final String CAPTION_FATHER_NAME = "Πατρώνυμο";
    public static final String CAPTION_AMETAA = "Α.Μ.Ε.Τ.Α.Α";
    public static final String CAPTION_MOB = "Κινητό";
    public static final String CAPTION_EMAIL = "Email";
    public static final String CAPTION_SPECIALTY = "Ειδικότητα";
    public static final String CAPTION_CHRONIC = "Χρόνια Πάθηση";
    public static final String CAPTION_PRESC_DATE = "Ημ/νια Έκδοσης";
    public static final String CAPTION_BEGIN_DATE = "Ημ/νια Έναρξης Συνταγής";
    public static final String CAPTION_END_DATE = "Ημ/νια Λήξης Συνταγής";
    public static final String CAPTION_DIAGNOSIS_COMMENTS = "Σχόλια";
    public static final String CAPTION_MEDICINE = "Περιγραφή Φαρμάκου";
    public static final String CAPTION_QUANTITY = "Ποσότητα";
    public static final String CAPTION_WAY_OF_INPUT = "Οδός χορήγησης";
    public static final String CAPTION_FREQUENCY = "Συχνότητα";
    public static final String CAPTION_DURATION = "Διαρκεια";
    public static final String CAPTION_PRESCRIBED = "Συνταγογραφήθηκε";// </editor-fold>

    private String[] prescStrings;
    private String[] prescHeaderStrings;
    private Object patientObject;
    private M006Controller controller;
    private Button viewFinButton;
    private Button viewPendButton;
    private Table finalizedPrescTable;
    private Table pendingPrescTable;
    private Button backButton;

    public M006View(AbstractModule module) {
        super(module);
    }

    public M006View(AbstractModule module, Object[] args) {
        super(module, args);
        this.patientObject = args[0];
    }

    @Override
    public ComponentContainer getView() {
        controller = new M006Controller(patientObject, module);
        initializeFormStrings();
        initializeComponents();
        initializeListeners();
        return viewLayout;
    }

    public void initializeComponents() {
        //finalized Prescription Panel
        Panel finalizedPrescPanel = new Panel("Λίστα ολοκληρωμένων συνταγών");
        //pending Prescription Panel
        Panel pendingPrescPanel = new Panel("Λίστα εκκρεμών συνταγών");
        //finilized table
        finalizedPrescTable = new Table();
        finalizedPrescTable.setImmediate(true);
        finalizedPrescTable.setWidth("745px");
        finalizedPrescTable.setSelectable(true);
        finalizedPrescTable.setContainerDataSource(controller.getFinalizedPrescDataSource());
        finalizedPrescTable.setVisibleColumns(prescStrings);
        finalizedPrescTable.setColumnHeaders(prescHeaderStrings);
//        HorizontalLayout viewButtonLayout = new HorizontalLayout();
        viewFinButton = new Button("Προβολή Συνταγής");
        viewFinButton.setEnabled(false);
        //pending table
        pendingPrescTable = new Table();
        pendingPrescTable.setImmediate(true);
        pendingPrescTable.setWidth("745px");
        pendingPrescTable.setSelectable(true);
        pendingPrescTable.setContainerDataSource(controller.getPendingPrescDataSource());
        pendingPrescTable.setVisibleColumns(prescStrings);
        pendingPrescTable.setColumnHeaders(prescHeaderStrings);
        viewPendButton = new Button("Προβολή Συνταγής");
        viewPendButton.setEnabled(false);

        //back button
        backButton = new Button(M005View.CAPTION_BACK);

        //add view finilized Button to horizontal layout
//        viewButtonLayout.addComponent(viewButton);
        //add tables to panels
        finalizedPrescPanel.addComponent(finalizedPrescTable);
//        finalizedPrescPanel.addComponent(viewButtonLayout);
        finalizedPrescPanel.addComponent(viewFinButton);
        pendingPrescPanel.addComponent(pendingPrescTable);
        pendingPrescPanel.addComponent(viewPendButton);
        //add Panels to viewLayout
        viewLayout.addComponent(finalizedPrescPanel);
        viewLayout.addComponent(pendingPrescPanel);
        viewLayout.addComponent(backButton);

    }

    private void initializeListeners() {
        finalizedPrescTable.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                viewFinButton.setEnabled(finalizedPrescTable.getValue() != null);
            }
        });

        pendingPrescTable.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                viewPendButton.setEnabled(pendingPrescTable.getValue() != null);
            }
        });

        viewFinButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                PrescFinalReadOnly prescriptionView = new PrescFinalReadOnly(module, new Object[]{controller.getPatient(), controller.getPrescriptionId(finalizedPrescTable.getValue())});
                Window finPrescWindow = new Window("Συνταγή");
                finPrescWindow.setHeight("750px");
                finPrescWindow.setWidth("850px");
                finPrescWindow.setModal(true);
                finPrescWindow.setResizable(false);
                finPrescWindow.addComponent(prescriptionView.getView());
                module.getApplication().getMainWindow().addWindow(finPrescWindow);
            }
        });

        viewPendButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                PrescPending prescriptionView = new PrescPending(module,
                        new Object[]{controller.getPatient(),
                        controller.getPrescriptionId(pendingPrescTable.getValue())});
                Window pendPrescWindow = new Window("Συνταγή");
                pendPrescWindow.setHeight("750px");
                pendPrescWindow.setWidth("850px");
                pendPrescWindow.setModal(true);
                pendPrescWindow.setResizable(false);
                pendPrescWindow.addComponent(prescriptionView.getView());
                module.getApplication().getMainWindow().addWindow(pendPrescWindow);
            }
        });

        backButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.DOCTOR_TYPE, patientObject});
            }
        });
    }

     private void initializeFormStrings() {
        prescStrings = new String[]{
                    "t002Doctor",
                    "t005Patient",
//                    "prescDate",
//                    "beginDate",
//                    "endDate",
//                    "chronic",
                    "t012Diagnosis",
                    "diagnosisComments"
                };
        prescHeaderStrings = new String[]{
                    CAPTION_DOCTOR,
                    CAPTION_PATIENT,
//                    CAPTION_PRESC_DATE,
//                    CAPTION_BEGIN_DATE,
//                    CAPTION_END_DATE,
//                    CAPTION_CHRONIC,
                    CAPTION_DIAGNOSIS,
                    CAPTION_DIAGNOSIS_COMMENTS
                };
    }
}
